import { reactive } from 'vue'
import { addPurchaseOrderData, updatePurchaseOrderData, getPurchaseOrderDataForm } from '@/api/warehouse/purchase-order'
import { getOptionList } from '@/api/base/brand'
import { getWarehouseOptionList } from '@/api/base/warehouse'
import { getProductOptionList, getProductDataForm } from '@/api/base/product'
import { updatePurchaseOrderProductStatus } from '@/api/warehouse/purchase-order/purchase-order-detail'
export function useDetail(getFormValues: Function) {
  const status = reactive({
    saving: false,
    loading: false
  })

  const createOrUpdate = async () => {
    const values = getFormValues()
    if (status.saving) return false
    status.saving = true
    try {
      values?.code ? await updatePurchaseOrderData(values.code, values) : await addPurchaseOrderData(values)
      status.saving = false
      return true
    } catch (err) {
      status.saving = false
      return false
    }
  }
  // 根据编码获取产品详情
  const getProductByCode = async (code: string) => {
    return getProductDataForm(code)
  }

  // 获取产品下拉列表
  const getProductOptions = async () => {
    return getProductOptionList()
  }

  // 获取采购单详情
  const getDataById = async (userCode: any) => {
    return getPurchaseOrderDataForm(userCode)
  }

  // 获取品牌方列表
  const getBrandOptions = async () => {
    return getOptionList()
  }

  // 获取仓库列表
  const getWarehouseList = async () => {
    return getWarehouseOptionList()
  }

  const updatePurchaseOrderDetailProductStatus = async (code: any) => {
    return updatePurchaseOrderProductStatus(code)
  }
  return {
    status,
    createOrUpdate,
    getDataById,
    getBrandOptions,
    getWarehouseList,
    getProductOptions,
    getProductByCode,
    updatePurchaseOrderDetailProductStatus
  }
}
